#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cstdio>

using namespace std;

class Datapack {
public:
	int data[3];
};

class Record {
public:
	int id, clocks;
	Datapack data;
};

istream& operator >> (istream& out, Datapack& data) {
	return out >> data.data[0] >> data.data[1] >> data.data[2];
}

istream& operator >> (istream& out, Record& rec) {
	return out >> rec.id >> rec.clocks >> rec.data;
}

ostream& operator << (ostream& out, Datapack& data) {
	return out << data.data[0] << " " << data.data[1] << " " << data.data[2];
}

ostream& operator << (ostream& out, Record& rec) {
	return out << rec.id << " " << rec.clocks << " \t " << rec.data;
}

Record depthRec[10000];
Record grayRec[10000];
int depthRecSize = 0;
int grayRecSize = 0;
int match[10000];

int main(void)
{
	ifstream inF;

	inF.open("depth_center.txt");
	while (!inF.eof()) {
		inF >> depthRec[depthRecSize++];
	}
	inF.close();

	inF.open("gray_center.txt");
	while (!inF.eof()) {
		inF >> grayRec[grayRecSize++];
	}
	inF.close();

	for (int i=0; i<10000; ++i)
		match[i] = -1;

	for (int i=0, j=0; i<grayRecSize && j<depthRecSize; ++i) {
		while (depthRec[j].clocks < grayRec[i].clocks-50) {
			if (++j>=depthRecSize) break;
		}
		if (j>=depthRecSize) break;

		int minV=1000, minI=-1;
		for (int k=j; k<depthRecSize; ++k) {
			int t = abs(depthRec[k].clocks-grayRec[i].clocks);
			if (t < minV) {
				minV = t;
				minI = k;
			}
		}

		if (minI != -1) {
			match[i] = minI;
			j = minI + 1;
		}
	}

	for (int i=0; i<grayRecSize; ++i) {
		cout << grayRec[i].clocks << " " << depthRec[match[i]].clocks << endl;
	}

	ofstream outF;
	outF.open("new_depth_center.txt");
	for (int i=0; i<grayRecSize; ++i)
		if (match[i] != -1) {
			outF << depthRec[match[i]] << endl;
		}
	outF.close();

	outF.open("new_gray_center.txt");
	for (int i=0; i<grayRecSize; ++i)
		if (match[i] != -1) {
			outF << grayRec[i] << endl;
		}
	outF.close();
	


	return 0;
}